<?php

namespace JsonLowCode\Support;

use JsonLowCode\JsBase;

/**
 * JsEvent
 * @method static JsEvent make(bool $isFunction = true)
 */
class JsEvent extends JsBase
{
    protected string $event = "this.\$event";

    protected array $options = [];

    /**
     * @param bool $isFunction 是否函数包裹
     */
    public function __construct(bool $isFunction = true){
        $this->event = Js::event();
        $this->setJsFunction($isFunction);
    }

    /**
     * @return string
     */
    public function all(): string
    {
        $this->script = $this->event.".all()";
        return $this->toString();
    }

    /**
     * @param string $key
     * @param string $value
     * @return string
     */
    public function on(string $key = "", string $value = ""): string
    {
        $key = $this->jsFrom($key);
        $value = $this->jsFrom($value);
        $this->script = $this->event.".on({$key},{$value})";
        return $this->toString();
    }

    /**
     * @param $key
     * @return string
     */
    public function emit($key): string
    {
        $key = $this->jsFrom($key);
        $this->script = $this->event.".emit({$key})";
        return $this->toString();
    }

    /**
     * @return string
     */
    public function off(): string
    {
        $this->script = $this->event.".off()";
        return $this->toString();
    }

}
